﻿using System;
using System.Collections.Generic;
using System.Linq;
using Common.Extension;
using Common.Model;
using Common.util;
using Dapper;

namespace Common.Dao
{
    public class GszDao
    {
        //保存新闻
        public static void BatchSaveNews(List<GszNews> news)
        {
            String sql =
                @"insert ignore `news_gsz`(title,content,stock,content_id,crawling_time) " +
                "VALUES (@Title,@Content,@Stock,@ContentId,@CrawlingTime)";
            using (var conn = DbUtils.GetHotTopicConnection())
            {
                conn.InsertMultiple(sql, news);
            }
        }

        //获取最新一条新闻
        public static GszNews GetLastNews()
        {
            string sql =
                "SELECT title,content,stock,content_id AS ContentId,crawling_time As CrawlingTime FROM `news_gsz` " +
                "ORDER BY crawling_time DESC LIMIT 1";

            using (var conn = DbUtils.GetHotTopicConnection())
            {
                var list = conn.Query<GszNews>(sql).ToList();
                return list.Any() ? list.First() : null;
            }
        }
        

        //获取新闻内容
        public static List<GszNews> GetAllNews()
        {
            string sql =
                "SELECT title,content,stock,content_id AS ContentId,crawling_time As CrawlingTime FROM `news_gsz` " +
                "ORDER BY site_time";
            using (var conn = DbUtils.GetHotTopicConnection())
            {
                return conn.Query<GszNews>(sql)?.ToList();
            }
        }

    }
}